﻿function FileInfo() {
    this.get_type = function() {
        if (typeof this.t == "object") {
            return this.t;
        }
        this.o.Position = 0;
        var s = this.o.ReadText(20), t = {}, pid = "";
        if ((pid = s.substr(0, 2)) == "BM") {
            t.type = "bmp";
        } else if (pid == "MZ") {
            t.type = "exe"; t.format = "pe";
        } else if (pid == "PK") {
            t.type = "zip";
        } else if ((pid = s.substr(0, 3)) == "GIF") {
            t.type = "gif";
        } else if (pid == "II*") {
            t.type = "tif";
        } else if (pid == "CWS" || pid == "FWS") {
            t.type = "swf"; t.format = pid.toLowerCase();
        } else if (pid == "ID3") {
            t.type = "mp3";
        } else if ((pid = s.substr(0, 4)) == "\x89PNG") {
            t.type = "png";
        } else if (pid == "RIFF") {
            t.type = "wav";
        } else if (pid == "ITSF") {
            t.type = "chm";
        } else if (pid == "\x25PDF") {
            t.type = "pdf";
        } else if (pid == "Rar\x21") {
            t.type = "rar";
        } else if (s.substr(6, 4) == "JFIF") {
            t.type = "jpg"; t.format = "jfif";
        } else if (pid == "\xFF\xD8\xFF\xE1") {
            t.type = "jpg";
        } else if (pid == ".RMF") {
            t.type = "rm";
        } else if (pid == "0\x26\xB2u") {
            t.type = "wma";
        } else if (s.substr(4, 12) == "Standard Jet") {
            t.type = "mdb";
        } else if (s.substr(0, 12) == "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1\x00\x00\x00\x00") {
            t.type = "mso";
        } else if (s.substr(2, 3) == "AMR") {
            t.type = "amr";
        } else if (s.substr(0, 3) == "\xEF\xBB\xBF") {
            t.type = "txt"; t.format = "utf-8";
        } else if ((pid = s.substr(0, 2)) == "\xFF\xFE") {
            t.type = "txt"; t.format = "utf-16le";
        } else if (pid == "\xFE\xFF") {
            t.type = "txt"; t.format = "utf-16be";
        } else if (s.substr(0, 4) == "\x84\x31\x95\x33") {
            t.type = "txt"; t.format = "gb18030";
        } else {
            t.type = "!!!";
        }
        this.type = t;
        return t;
    };
    this.get_info = function() {
        var w, h, t1, t2;
        var c_int = this.c_int, o = this.o, binary = this.binary;
        if (typeof this.type != "object") {
            this.get_type();
        }
        switch (this.type.type) {
            case "png":
                o.Position = 18;
                w = this.c_int_le(o.ReadText(2));
                h = o.ReadText(2);
                h = this.c_int_le(o.ReadText(2));
                break;
            case "gif":
                o.Position = 6;
                w = this.c_int_be(o.ReadText(2));
                h = this.c_int_be(o.ReadText(2));
                break;
            case "jpg":
                o.Position = 2;
                while (c_int(o.ReadText(1)) == 0xFF) {
                    w = c_int(o.ReadText(1));
                    if (w >= 0xC0 && w <= 0xCF) break;
                    o.Position += this.c_int_le(o.ReadText(2));
                }
                o.Position += 3;
                h = this.c_int_le(o.ReadText(2));
                w = this.c_int_le(o.ReadText(2));
                break;
            case "bmp":
                o.Position = 18;
                w = this.c_int_be(o.ReadText(4));
                h = this.c_int_be(o.ReadText(4));
                break;
            case "swf":
                if (this.type.format == "cws") { w = 0; h = 0; break; }
                o.Position = 8;
                //*
                t1 = c_int(o.ReadText(1));
                t2 = this.hex(o.ReadText(8));
                switch (t1) {
                    case 0x50:
                        h = parseInt(t2.substr(0, 4), 16) / 10;
                        w = parseInt(t2.substr(5, 4), 16) / 10; break;
                    case 0x58:
                        h = parseInt(t2.substr(1, 4), 16) / 40;
                        w = parseInt(t2.substr(6, 4), 16) / 10; break;
                    case 0x60:
                        h = parseInt(t2.substr(1, 4), 16) / 10;
                        w = parseInt(t2.substr(7, 4), 16) / 10; break;
                    case 0x68:
                        h = parseInt(t2.substr(2, 4), 16) / 40;
                        w = parseInt(t2.substr(8, 4), 16) / 10; break;
                    case 0x70:
                        h = parseInt(t2.substr(2, 4), 16) / 10;
                        w = parseInt(t2.substr(9, 4), 16) / 10; break;
                    case 0x78:
                        h = parseInt(t2.substr(3, 4), 16) / 40;
                        w = parseInt(t2.substr(10, 4), 16) / 10; break;
                    case 0x80:
                        h = parseInt(t2.substr(3, 4), 16) / 10;
                        w = parseInt(t2.substr(11, 4), 16) / 10; break;
                    case 0x88:
                        h = parseInt(t2.substr(2, 4), 16) / 10;
                        w = parseInt(t2.substr(12, 4), 16) / 10; break;
                }
                /*/
                t1 = c_int(o.ReadText(1)) >> 3;
                o.Position--;
                t2 = Math.ceil(t1 / 2) + 1;
                t2 = binary(o.ReadText(t2)).substr(5);
                w = parseInt(Math.abs(parseInt(t2.substr(1 * t1, t1), 2) - parseInt(t2.substr(0 * t1, t1), 2)) / 20);
                h = parseInt(Math.abs(parseInt(t2.substr(3 * t1, t1), 2) - parseInt(t2.substr(2 * t1, t1), 2)) / 20);
                //*/
                break;
            default: w = 0; h = 0;
        }
        this.size = { "width": w, "height": h };
        return this.size;
    };
    this.open = function(path) {
        with (this.o) {
            if (this.o.State) this.o.Close();
            Type = 2;
            Mode = 3;
            CharSet = "iso-8859-15";
            Open();
            LoadFromFile(path);
        }
        this.path = path;
        this.type = undefined;
        this.size = undefined;
    };
    this.close = function() {
        this.o.close();
    };
    this.c_int = function(v) {
        var c = v.charCodeAt(0);
        switch (c) {
            case 0x20AC: return 0xA4;
            case 0x160: return 0xA6;
            case 0x161: return 0xA8;
            case 0x17D: return 0xB4;
            case 0x17E: return 0xB8;
            case 0x152: return 0xBC;
            case 0x153: return 0xBD;
            case 0x178: return 0xBE;
            default: return c;
        }
    };
    this.c_int_be = function(v) {
        var c = 0, n = 0;
        for (var i = v.length - 1; i > -1; i--) {
            n = n * 256 + this.c_int(v.charAt(i));
        }
        return n;
    };
    this.c_int_le = function(v) {
        var c = 0, n = 0;
        for (var i = 0; i < v.length; i++) {
            n = n * 256 + this.c_int(v.charAt(i));
        }
        return n;
    };
    this.hex = function(v) {
        var h = "", p;
        for (var i = 0; i < v.length; i++) {
            p = this.c_int(v.charAt(i)) | 0x100;
            h = h + p.toString(16).slice(-2);
        }
        return h;
    };
    this.binary = function(v) {
        var b = "", p;
        for (var i = 0; i < v.length; i++) {
            p = this.c_int(v.charAt(i)) | 0x100;
            b = b + p.toString(2).slice(-8);
        }
        return b;
    };
    this.o = Server.CreateObject("Adodb.Stream");
}